package com.gxy.learn.design.model.behaviour.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: gaoxueyong
 * @Date: 2022/11/8 18:07
 */
public class SubScriptionSubject implements Subject{
    private List<Observer> observers = new ArrayList<>();

    /**
     * 主题订阅
     *
     * @param observer
     * @return void
     * @Author gaoxueyong
     * @Date 6:04 下午 2022/11/8
     * @Param [observer]
     */
    @Override
    public void attch(Observer observer) {
       this.observers.add(observer);
    }

    /**
     * 取消主题订阅
     *
     * @param observer
     * @return void
     * @Author gaoxueyong
     * @Date 6:04 下午 2022/11/8
     * @Param [observer]
     */
    @Override
    public void detach(Observer observer) {
        this.observers.remove(observer);
    }

    /**
     * 消息通知
     *
     * @param message
     * @return void
     * @Author gaoxueyong
     * @Date 6:04 下午 2022/11/8
     * @Param [observer]
     */
    @Override
    public void notify(String message) {
        for (Observer ob : observers) {
            ob.update(message);
        }
    }
}
